﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ClassLibrary1;
using static ClassLibrary1.CMDUtil;

namespace WpfStudy
{
    /// <summary>
    /// PictrueView.xaml 的交互逻辑
    /// </summary>
    public partial class PictrueView : Window
    {

        static string PrefixUrl = "http://127.0.0.1:8029";

        static List<ItemInfo> items = null;

        static int count = 0;

        public PictrueView()
        {
            InitializeComponent();
            Loaded += PictrueView_Loaded; 
        }

        private void PictrueView_Loaded(object sender, RoutedEventArgs e)
        {
            //string[] cmds = new string[] { "adb forward tcp:8029 tcp:8099" };
            string v = CMDUtil.ExecuteOutCmd("forward tcp:8029 tcp:8099", @"D:\Program Files (x86)\AnyTrans\AnyTrans\adb.exe");
            //string v = ShellCmd.SyncCmdExcute("forward tcp: 8029 tcp: 8099");
            Console.WriteLine("运行cmd={0}", v);

            items = HttpUtil.GetPhotoAlbums($"{PrefixUrl}/photoalbumlist");

            Console.WriteLine("进行图片资源加载...加载{0}张图片", items.Count);

            

            ImageView.Stretch = Stretch.Uniform;
        }

        private void BtnUp_Click(object sender, RoutedEventArgs e)
        {
            count = count == 0 ? 0 : --count;
            if (null == items || items.Count == 0 )
            {
                LoadInfo();
            }
            
            int index = count;
            Console.WriteLine("切换上一张图片index={0}",index);
            string imageUrl = string.Format($"{PrefixUrl}{items[index].thumbnailUrl}");
            Console.WriteLine(imageUrl);
            ImageView.Source = new BitmapImage(new Uri(imageUrl));
        }

        private void LoadInfo()
        {
            items = HttpUtil.GetPhotoAlbums($"{PrefixUrl}/photoalbumlist");
           
            Console.WriteLine("进行图片资源加载...加载{0}张图片",items.Count);

        }

       

        private void BtnNext_Click(object sender, RoutedEventArgs e)
        {
            count = count >= items.Count-1 ? (items.Count - 1) : ++count;
            if (null == items || items.Count == 0)
            {
                LoadInfo();
            }
            int index = count;
            Console.WriteLine("切换下一张图片index={0}", index);
            string imageUrl = string.Format($"{PrefixUrl}{items[index].thumbnailUrl}");
            Console.WriteLine(imageUrl);
            ImageView.Source = new BitmapImage(new Uri(imageUrl));
        }
    }
}
